#include "test_rtc.h"
#include "cm_uart.h"
#include "cm_demo_uart.h"
#include "string.h"
#include "stdlib.h"



void cm_test_rtc_operation(unsigned char*cmd[],int len)
{
    if(0 == strncmp("SET_SYS_TIME",cmd[2],strlen("SET_SYS_TIME")))
    {
        uint64_t second = strtoull(cmd[3],(void**)0,10);
        int32_t rtc_set_current_time = cm_rtc_set_current_time(second);
        cm_demo_printf("cm_rtc_set_current_time ret:%d\n",rtc_set_current_time);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,rtc_set_current_time,cm_rtc_set_current_time);
    }else if(0 == strncmp("GET_CUR_TIME",cmd[2],strlen("GET_CUR_TIME")))
    {
        uint64_t rtc_get_current_time = cm_rtc_get_current_time();
        cm_demo_printf("cm_rtc_get_current_time ret:%llu\n",rtc_get_current_time);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_UINT32(0,rtc_get_current_time >> 32,cm_rtc_get_current_time);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_UINT32(0,rtc_get_current_time,cm_rtc_get_current_time);

    }else if(0 == strncmp("SET_TIME_ZONE",cmd[2],strlen("SET_TIME_ZONE")))
    {
        int32_t timezone = strtol(cmd[3],(void**)0,10);
        int32_t rtc_set_timezone = cm_rtc_set_timezone(timezone);
        cm_demo_printf("cm_rtc_set_timezone ret:%d\n",rtc_set_timezone);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,rtc_set_timezone,cm_rtc_set_timezone);
    }
    else if(0 == strncmp("GET_TIME_ZONE",cmd[2],strlen("GET_TIME_ZONE")))
    {
        int32_t rtc_get_timezone = cm_rtc_get_timezone();
        cm_demo_printf("cm_rtc_get_timezone ret:%d\n",rtc_get_timezone);
        OCTEST_FUNC_EXPECT_GREATER_OR_EQUAL_INT32(-12,rtc_get_timezone,cm_rtc_get_timezone);
        OCTEST_FUNC_EXPECT_LESS_OR_EQUAL_INT32(12,rtc_get_timezone,cm_rtc_get_timezone);
    }
    else if(0 == strncmp("SET_GET_TIME_ZONE",cmd[2],strlen("SET_GET_TIME_ZONE")))
    {

        for(int timezone = -12;timezone <= 12; timezone++)
        {
            int32_t rtc_set_timezone = cm_rtc_set_timezone(timezone);
            cm_demo_printf("cm_rtc_set_timezone ret:%d\n",rtc_set_timezone);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(0,rtc_set_timezone,cm_rtc_set_timezone);
            int32_t rtc_get_timezone = cm_rtc_get_timezone();
            cm_demo_printf("cm_rtc_get_timezone ret:%d\n",rtc_get_timezone);
            OCTEST_FUNC_EXPECT_EQUAL_INT32(timezone,rtc_get_timezone,cm_rtc_get_timezone);
        }
        
    }
    else if(0 == strncmp("GET_TIME_OF_DAY",cmd[2],strlen("GET_TIME_OF_DAY")))
    {
        static struct timeval
        {
            long tv_sec;
            long tv_usec;
        };
        struct timeval tv = {0};
        int32_t timeofday = gettimeofday(&tv, NULL);
        OCTEST_FUNC_EXPECT_EQUAL_INT32(0,timeofday,gettimeofday);
        cm_demo_printf("tv_sec:%lld, tv_usec:%ld\n",tv.tv_sec,tv.tv_usec);

        time_t timer = 0;
        time_t timess = time (&timer);
        cm_demo_printf("timer:%lld, %lld\n",timer,timess);
    }
    else{
        cm_demo_printf("param error!\n");
    }
}